package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ab.soap.AbSoapUtil;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.UffjqgdList;
import com.htandroid.htandroid.entity.UffjqgdList_detail;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.BuildQgdDjdetailXMLService;
import com.htandroid.htandroid.webservice.XMLQgdDjdetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

/**
 * Created by lkp on 2015/11/29.
 */
public class qgdsp_detaillist extends base_index {
    private ArrayList<UffjqgdList_detail> qgdlist_detail;
    private UffjqgdList qgdtitle;
    String icurrentrow;//记录上一页的当前行
    private String listtype = "0";

    @AbIocView(id = R.id.tv_log, click = "btnClick")
    TextView tv_log;
    @AbIocView(id = R.id.tv_undo)
    TextView tv_undo;
    @AbIocView(id = R.id.tv_return, click = "btnClick")
    TextView tv_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_qgddj_detaillist);
        initview("请购单明细");
        initview2();
        createrl(qgdlist_detail);
    }

    public void btnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_log:
                log_view(v);
                break;
            case R.id.tv_return:
                qgd_return(v);
                break;
        }
    }

    //初始化2
    @Override
    public void initview2() {
        titleBar.getLogoView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                intent.putExtra("listrow", icurrentrow);
                //因为数据可能发生变化，所以需要把数据返回
                bundle.putSerializable("detaillist", qgdlist_detail);
                bundle.putSerializable("qgdtitle", qgdtitle);
                intent.putExtras(bundle);
                setResult(R.layout.layout_qgddj_detaillist / 300000, intent);  //设置返回结果
                finish();
            }
        });

        //获取传递的对象参数
        qgdlist_detail = (ArrayList<UffjqgdList_detail>) getIntent().getSerializableExtra("detaillist");
        qgdtitle = (UffjqgdList) getIntent().getSerializableExtra("qgdtitle");
        icurrentrow = getIntent().getExtras().getString("currentrow");
        listtype = getIntent().getExtras().getString("listtype");
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                intent.putExtra("listrow", String.valueOf(id));
                //因为数据可能发生变化，所以需要把数据返回
                bundle.putSerializable("detaillist", qgdlist_detail);
                bundle.putSerializable("qgdtitle", qgdtitle);
                intent.putExtras(bundle);
                setResult(R.layout.layout_qgddj_detaillist / 300000, intent);  //设置返回结果
                finish();
            }
        });

        tv_undo.setVisibility(View.GONE);//恢复按钮隐藏
//        if (listtype.equals("1")) {
//            tv_return.setVisibility(View.GONE);
//        } else {
//            tv_return.setVisibility(View.VISIBLE);
//        }
    }

    public void createrl(ArrayList<UffjqgdList_detail> cqgdlists) {
        adatperDataList.clear();
        ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < cqgdlists.size(); i++) {
            //增加一个行选择状态初始化
            if (i == Integer.valueOf(icurrentrow)) {
                sa.getIsSelected().put(i, true);
            } else {
                sa.getIsSelected().put(i, false);
            }

            UffjqgdList_detail qgds = new UffjqgdList_detail();
            qgds = cqgdlists.get(i);

            Map<String, Object> map = new HashMap<String, Object>();

            map.put("index", (i + 1));
            map.put("cvouchid", "存货名称：" + qgds.getCinvname());
            map.put("cdept", "单位：" + Basenvo.space_add(qgds.getCinvm_unit(), 12, 0) + "规格：" + Basenvo.space_add(qgds.getCinvstd(), 12, 1));
            map.put("cnocheckreason", "数量：" + Basenvo.space_add(Basenvo.customFormat("######.00##", qgds.getIquantity()), 12, 0) + "单价：" + Basenvo.space_add(Basenvo.customFormat("######.00##", qgds.getIprice()), 12, 1));
            map.put("ddate", "供应商：" + qgds.getCvenname());
            //更多数据
            map.put("data5", "待选供应商1：" + qgds.getCvennamep1());
            map.put("data6", "待选供应商2：" + qgds.getCvennamep2());
            map.put("data7", "待选供应商3：" + qgds.getCvennamep3());
            map.put("data8", "");
            dataListForInit.add(map);
        }
        toolDataProvider.initData(dataListForInit);
        loadList();
        //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
        //swipeRefreshLayout.setRefreshing(false);
    }

    //请购单退回
    public void qgd_return(View v) {
        if (!preeditcheck()) {
            return;
        }
        Intent intent = new Intent(getApplicationContext(), Qgddj_detail_return.class);
        //传递单据号
        intent.putExtra("cvouchid", qgdtitle.getCvouchid());// 设置传递数据
        intent.putExtra("ztid", qgdtitle.getCacc_id());
        startActivityForResult(intent, R.layout.layout_qgddj_detail_return / 400000);// 执行Intent操作
    }

    //请购单退回的处理函数
    public void qgddj_return_ok(String recharname, String restep, String renote) {
        //loadingDialog.loading(this);
        //退回操作
        AbDialogUtil.showProgressDialog(qgdsp_detaillist.this, 0, "正在加载...");
        String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;
        String[] parms = {"user", "cacc_id", "iyear", "cvouchid", "charname", "linecode", "restep", "recharname", "renote"};
        String[] vals = {User.nickName, qgdtitle.getCacc_id(), (String) myApplication.getGs_year(), qgdtitle.getCvouchid(), qgdtitle.getCchar(), qgdtitle.getCnocheckreason(), restep, recharname, renote};
        //生成xml
        BuildQgdDjdetailXMLService service = new BuildQgdDjdetailXMLService();
        String xmlstring = service.buildXML(qgdlist_detail, "qgddj_return", InstorageURL.NAME_SPACE, parms, vals, "in" + parms.length);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "qgddj_return", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    qgdlist_detail = XMLQgdDjdetail.paraseCommentInfors(result);
                    if (qgdlist_detail.size() > 0) {
                        String succount = qgdlist_detail.get(0).getSuccount();
                        if (succount.equals("0")) {
                            AbToastUtil.showToastInThread(getApplicationContext(), "请购单退回失败!");
                        } else {
                            AbToastUtil.showToastInThread(getApplicationContext(), "请购单退回成功!");
                            //退回完成应该是关闭当前页面返回到上层
                            Bundle bundle = new Bundle();
                            Intent intent = new Intent();
                            intent.putExtra("listrow", "-100");//行号,负100表示为退回.修改界面也同样需要返回
                            //因为数据可能发生变化，所以需要把数据返回
                            bundle.putSerializable("detaillist", qgdlist_detail);
                            bundle.putSerializable("qgdtitle", qgdtitle);
                            intent.putExtras(bundle);
                            setResult(R.layout.layout_qgddj_detaillist / 300000, intent);  //设置返回结果
                            finish();
                        }
                    } else {
                        AbToastUtil.showToastInThread(getApplicationContext(), "请购单退回失败!数据库错误!");
                    }
                    AbDialogUtil.removeDialog(qgdsp_detaillist.this);
                    //loadingDialog.loadingfinish();
                }
            }

            @Override
            public void onFailure(String result) {
                AbToastUtil.showToastInThread(getApplicationContext(), "请购单退回失败!");
                //loadingDialog.loadingfinish();
            }
        });
    }

    //日志查看
    public void log_view(View v) {
        Intent intent = new Intent(getApplicationContext(), Basenvo_splogview.class);
        //传递单据号
        intent.putExtra("cvouchid", qgdtitle.getCvouchid());// 设置传递数据
        intent.putExtra("ztid", qgdtitle.getCacc_id());
        intent.putExtra("tablename", "UFFJ_QGD_LOG");// 设置传递数据
        startActivityForResult(intent, R.layout.basenvo_splogview / 500000);// 执行Intent操作
    }

    public boolean preeditcheck() {
        if (!Basenvo.equal(qgdtitle.getCchecker(), "")) {
            AbToastUtil.showToastInThread(getApplicationContext(), "此单据已被审核，不能修改，请取消审核后再试!");
            return false;
        }
        /*20170214屏蔽，审批退回时不需要检测定价人或者暂存人。
        if ( ( (!Basenvo.equal(qgdtitle.getCprechecker(),User.nickName) && !Basenvo.equal(qgdtitle.getCprechecker(),"")) || (!Basenvo.equal(qgdtitle.getCprecheckertemp(),User.nickName) && !Basenvo.equal(qgdtitle.getCprecheckertemp(),"") ) ) && (!Basenvo.equal(glapp.getGs_option2().getQgd_dd_check(),"Y")) ){
            Toast.makeText(getApplicationContext(), "不能修改他人定价或暂存的单据！", Toast.LENGTH_SHORT).show();
            return false;
        }*/
        return true;
    }

    //信息返回
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            //请购单退回处理
            case R.layout.layout_qgddj_detail_return / 400000:
                if (data != null) {
                    //获取传递的对象参数
                    Bundle bundle = data.getExtras();
                    //获取返回值
                    String recharname = bundle.getString("recharname");
                    String restep = bundle.getString("restep");
                    String renote = bundle.getString("renote");
                    //进行请购单退回操作
                    qgddj_return_ok(recharname, restep, renote);
                }
                break;
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
